//
//  PlayDetailCollectionViewCell.m
//  MMLOOK
//
//  Created by dllo on 15/10/10.
//  Copyright © 2015年 武俊娥. All rights reserved.
//

#import "PlayDetailCollectionViewCell.h"
#import "Play.h"
#import "UIImageView+WebCache.h"
#import "PlayVideoViewController.h"


@interface PlayDetailCollectionViewCell ()

@property(nonatomic, retain)UIImageView *image;

@property(nonatomic, retain)UIImageView *backImage;
@property(nonatomic, retain)UILabel *nameLabel;
@property(nonatomic, retain)UILabel *timeLabel;
@property(nonatomic, retain)UILabel *detailLabel;

@end


@implementation PlayDetailCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.image = [[UIImageView alloc]init];
        self.image.userInteractionEnabled = YES;
        [self.contentView addSubview:self.image];
        [_image release];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(playVideo)];
        [self.image addGestureRecognizer:tap];
        [tap release];
        
        self.backImage =[[UIImageView alloc]init];
        [self.contentView addSubview:self.backImage];
        [_backImage release];
        
        self.nameLabel = [[UILabel alloc]init];
//        self.nameLabel.textAlignment = NSTextAlignmentCenter;
        self.nameLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
        self.nameLabel.textColor = [UIColor whiteColor];
        [self.backImage addSubview:self.nameLabel];
        [_nameLabel release];
        
        
        self.timeLabel = [[UILabel alloc]init];
//        self.timeLabel.textAlignment = NSTextAlignmentCenter;
        self.timeLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
        self.timeLabel.textColor = [UIColor whiteColor];
        [self.backImage addSubview:self.timeLabel];
        [_timeLabel release];
        
        self.detailLabel =[[UILabel alloc]init];
        self.detailLabel.numberOfLines = 0;
        self.detailLabel.textColor = [UIColor whiteColor];
        self.detailLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
        [self.backImage addSubview:self.detailLabel];
        [_detailLabel release];
        
        
    }
    
    return self;
}

-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    
    self.image.frame = CGRectMake(0, 0, layoutAttributes.frame.size.width, layoutAttributes.frame.size.width);
    
    self.backImage.frame = CGRectMake(0, layoutAttributes.frame.size.width, layoutAttributes.frame.size.width, layoutAttributes.frame.size.height - layoutAttributes.frame.size.width);
    
    self.nameLabel.frame = CGRectMake(10, 10, layoutAttributes.frame.size.width, 30);
    
    self.timeLabel.frame = CGRectMake(10 , 40, layoutAttributes.frame.size.width, 20);
    
    self.detailLabel.frame = CGRectMake(10, 60, layoutAttributes.frame.size.width - 20, self.backImage.frame.size.height - 60);
    
}


-(void)setPlay:(Play *)play
{
    if (_play != play) {
        [_play release];
        _play = [play retain];
    }
    
    self.image.contentMode = UIViewContentModeScaleAspectFill;
    
    [self.image sd_setImageWithURL:[NSURL URLWithString:play.coverForFeed] placeholderImage:[UIImage imageNamed:@""]];

    [self.backImage sd_setImageWithURL:[NSURL URLWithString:play.coverBlurred] placeholderImage:[UIImage imageNamed:@""]];

    self.nameLabel.text = play.title;
    
    float time = [play.duration floatValue];
    NSInteger fen = time / 60;
    NSInteger miao = time - fen * 60;
    
    self.timeLabel.text = [NSString stringWithFormat:@"#%@  /  %ld' %ld\" ", play.category,fen,miao];
    
    self.detailLabel.text = play.descriptionP;
    
}


-(void)playVideo
{
    PlayVideoViewController *play = [[PlayVideoViewController alloc]init];
    
    play.title = self.nameLabel.text;
    
    [self.root.navigationController pushViewController:play animated:YES];
    
    play.playUrl = self.play.playUrl;
    
    [play release];
}

@end
